/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { XPlatform } = require("../XPlatform");
const { X2DBeta } = require("./X2DInterface");

class XNodeFast {
    constructor(url) {
        if (!url.endsWith("/")) {
            url += "/";
        }
        this.url_ = url;
        this.project_file_ = url + "project.json";
        this.data_ = null;
        this.ID_NODE_MAP = {};
        this.PATH_NODE_MAP = {};

        XPlatform.LoadDataFromUrl(XPlatform.LOAD_TYPE_JSON,
            this.project_file_, (data) => {
                let node = {
                    name: "root",
                    id: 0,
                    type: "branch",
                    parent: null,
                    rect: { x: 0, y: 0, w: 0, h: 0 },
                    child: data.data,
                    url: url,
                }
                this.Load(node, null);
                this.data_ = node;
            })
    }
    Load(node, parent) {
        /**
         * // 1,得到最大id，用于创建新node id
         * 2,重置image node的rid，重新载入资源
         * 3,初始化ID_NODE_MAP，可以通过ID找到NODE
         */
        this.ID_NODE_MAP[node.id] = node;
        node.parent = parent;
        if (node.type == "branch") {
            for (let c of node.child) {
                this.Load(c, node);
            }
        }
        if (node.type == "img") {
            node.rid = -1;
        }
        this.PATH_NODE_MAP[XNodeFast.GetPath(node)] = node;
    }
    IsLoaded() {
        if (this.data_) return true;
        return false;
    }

    static GetPath(node) {//获得完整路径
        if (node.parent == null) {
            return "";
        }
        let pth = node.name;
        while (node.parent.parent != null) {
            node = node.parent;
            pth = node.name + "/" + pth;
        }
        return pth;
    }
    GetNodeById(nid) {
        return this.ID_NODE_MAP[nid];
    }
    GetNodeByPath(pth) {
        return this.PATH_NODE_MAP[pth];
    }
    GetNode(nid_or_path) {
        switch (typeof nid_or_path) {
            case 'string':
                return this.GetNodeByPath(nid_or_path);
            case 'number':
                return this.GetNodeById(nid_or_path);
        }
    }
    QueryTable(nid_or_path, key, value, mult = false) {
        let tab = this.GetNode(nid_or_path);
        let idx = tab.head.indexOf(key);
        let name = "name_" + key;
        if (mult || value == null) {
            name += "_mult";
        }
        if (!(name in tab)) {
            tab[name] = {};
            for (let row of tab.rows) {
                if (mult || value == null) {
                    if (!(row[idx] in tab[name])) {
                        tab[name][row[idx]] = [];
                    }
                    tab[name][row[idx]].push(row);
                }
                else {
                    tab[name][row[idx]] = row;
                }
            }
        }
        if (value == null) {
            let ret = [];
            for (let v in tab[name]) {
                for (let row of tab[name][v]) {
                    let d = {}
                    for (let i = 0; i < tab.head.length; i++) {
                        d[tab.head[i]] = row[i];
                    }
                    ret.push(d);
                }
            }
            return ret;
        }
        else if (mult) {
            let ret = [];
            for (let row of tab[name][value]) {
                let d = {}
                for (let i = 0; i < tab.head.length; i++) {
                    d[tab.head[i]] = row[i];
                }
                ret.push(d);
            }
            return ret;
        }
        else {
            let ret = {};
            for (let i = 0; i < tab.head.length; i++) {
                ret[tab.head[i]] = tab[name][value][i];
            }
            return ret;
        }
    }
    static GetUrl(node) {
        while (node.parent != null) {
            node = node.parent;
        }
        return node.url;
    }
    static GetRoot(node) {//获得节点的根节点
        while (node.parent.parent != null) {
            node = node.parent;
        }
        return node;
    }
    static GetChild(node, childName) {
        for (let n of node.child) {
            if (n.name == childName) {
                return n;
            }
        }
        return null;
    }
}

module.exports = {
    XNodeFast
}